import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar_dialog.dart';
import 'package:xindong/widgets/common/xindong_app_bar_icon_button.dart';
import 'package:xindong/widgets/dialog/xindong_base_sheet.dart';

class XinDongStatureSheet extends StatefulWidget {
  final String? height; // 格式：180cm
  final String? weight; // 格式：50kg
  final String gender; // 性别
  final Function(String height, String weight) doneCallback;

  XinDongStatureSheet.show(
      {super.key, this.gender = Gender.none, required this.height, required this.weight, required this.doneCallback}) {
    showSmartSheet(sheet: this);
  }

  @override
  State<XinDongStatureSheet> createState() => _XinDongStatureSheetState();
}

class _XinDongStatureSheetState extends State<XinDongStatureSheet> {
  final TextStyle textStyle = const TextStyle(fontSize: 20, color: DZColors.titleColor);

  final List<String> heightList = [for (int i = 140; i <= 220; i++) '${i}cm'];

  final List<String> weightList = [for (int i = 30; i <= 100; i++) '${i}kg'];

  late int heightIndex = 0;

  late int weightIndex = 0;

  // 身高数据
  List<Widget> statureHeightList() {
    List<Widget> list = [];
    for (String text in heightList) {
      list.add(Center(child: Text(text, style: textStyle)));
    }
    return list;
  }

  // 体重数据
  List<Widget> statureWeightList() {
    List<Widget> list = [];
    for (String text in weightList) {
      list.add(Center(child: Text(text, style: textStyle)));
    }
    return list;
  }

  @override
  void initState() {
    super.initState();
    String? tempHeight = widget.height;
    String? tempWeight = widget.weight;
    if (TextUtil.isEmpty(widget.height) || TextUtil.isEmpty(widget.weight)) {
      // 男的随机默认160-180CM,50-70KG
      // 女的随机默认155-175CM,35-55KG
      int number = Random().nextInt(20 + 1);
      if (widget.gender == Gender.male) {
        tempHeight = "${number + 160}cm";
      } else {
        tempHeight = "${number + 155}cm";
      }
      if (widget.gender == Gender.male) {
        tempWeight = "${number + 50}kg";
      } else {
        tempWeight = "${number + 35}kg";
      }
    }
    heightIndex = heightList.indexOf(tempHeight ?? "180cm");
    weightIndex = weightList.indexOf(tempWeight ?? "50kg");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: DZColors.dialog,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          XinDongDialogAppBar(
            '选择身高体重',
            leading: XinDongAppBarIconButton(onTap: () => SmartDialog.dismiss()),
            onTapDone: () {
              widget.doneCallback(heightList[heightIndex], weightList[weightIndex]);
              SmartDialog.dismiss();
            },
          ),
          SizedBox(
            height: 260 + kAppBarHeight,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Container(
                          alignment: Alignment.center,
                          child: const Text(
                            "身高",
                            style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500, color: DZColors.titleColor),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 260,
                        child: CupertinoPicker(
                          itemExtent: kCellHeight,
                          scrollController: FixedExtentScrollController(initialItem: heightIndex),
                          onSelectedItemChanged: (position) {
                            heightIndex = position;
                          },
                          children: statureHeightList(),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Container(
                          alignment: Alignment.center,
                          child: const Text(
                            "体重",
                            style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500, color: DZColors.titleColor),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 260,
                        child: CupertinoPicker(
                          itemExtent: kCellHeight,
                          scrollController: FixedExtentScrollController(initialItem: weightIndex),
                          onSelectedItemChanged: (position) {
                            weightIndex = position;
                          },
                          children: statureWeightList(),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
